package com.example.othree.service.impl;

import com.example.othree.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {

    @Value("${data.file}")
    String filePath;

//    public List<String> getFileNamesByPage(int page, int pageSize) throws IOException{
//        File directory = new File(filePath);
//        if (!directory.exists() || !directory.isDirectory()) {
//            throw new IOException("Directory does not exist or is not a directory: ");
//        }
//
//        // 使用Commons IO获取目录下的所有文件
//        File[] files = directory.listFiles();
//        if (files == null) {
//            throw new IOException("Failed to list files in directory: ");
//        }
//
//        List<String> allFileNames = new ArrayList<>();
//        for (File file : files) {
//            if (file.isFile()) {
//                allFileNames.add(file.getName());
//            }
//        }
//
//        int fromIndex = (page - 1) * pageSize;
//        int toIndex = Math.min(fromIndex + pageSize, allFileNames.size());
//
//        return allFileNames.subList(fromIndex, toIndex);
//    }

    public List<String> getFileNames() throws IOException{
        File directory = new File(filePath);
        if (!directory.exists() || !directory.isDirectory()) {
            throw new IOException("Directory does not exist or is not a directory: " + filePath);
        }

        // 使用Commons IO获取目录下的所有文件
        File[] files = directory.listFiles();
        if (files == null) {
            throw new IOException("Failed to list files in directory: " + filePath);
        }

        List<String> allFileNames = new ArrayList<>();
        for (File file : files) {
            if (file.isFile()) {
                allFileNames.add(file.getName());
            }
        }

        return  allFileNames;
    }

    public FilePageInfo getFileInfosByPage(int page, int pageSize) throws IOException {
        File directory = new File(filePath);
        if (!directory.exists() || !directory.isDirectory()) {
            throw new IOException("Directory does not exist or is not a directory: " + filePath);
        }

        File[] files = directory.listFiles();
        if (files == null) {
            throw new IOException("Failed to list files in directory: " + filePath);
        }

        List<FileInfo> allFileInfos = new ArrayList<>();
        for (File file : files) {
            if (file.isFile()) {
                allFileInfos.add(new FileInfo(file.getName(), file.lastModified(), file.length()));
            }
        }

        int fromIndex = (page - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, allFileInfos.size());

        // 计算总数据量
        int total = allFileInfos.size();

        // 计算总页数
        int totalPages = (int)Math.ceil((double)total / pageSize);


//        return allFileInfos.subList(fromIndex, toIndex);
        return new FilePageInfo(totalPages, pageSize, page, total, new ArrayList<>(allFileInfos.subList(fromIndex, toIndex)));
    }

}
